<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="二、面向对象基础1. 内存分析1.1 Java虚拟机的内存Java虚拟机的内存可以分为三个区域：栈Stack、堆Heap、方法区method area（也是在堆中）。 1.1.1 栈的特点： 栈是方法执行的内存模型，每个方法被调用后都会创建一个栈帧，栈帧会保存该方法的局部变量、操作数、方法出口等； JVM会为每个线程都开辟一个栈，用于存放该线程执行方法的信息（实际参数、局部变量等），因此，栈时线">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-300集">
<meta property="og:url" content="http://yoursite.com/2018/11/13/Java-300集/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="二、面向对象基础1. 内存分析1.1 Java虚拟机的内存Java虚拟机的内存可以分为三个区域：栈Stack、堆Heap、方法区method area（也是在堆中）。 1.1.1 栈的特点： 栈是方法执行的内存模型，每个方法被调用后都会创建一个栈帧，栈帧会保存该方法的局部变量、操作数、方法出口等； JVM会为每个线程都开辟一个栈，用于存放该线程执行方法的信息（实际参数、局部变量等），因此，栈时线">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://yoursite.com/2018/11/13/Java-300集/assets/java内存模型.jpg">
<meta property="og:updated_time" content="2018-11-21T11:13:50.801Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-300集">
<meta name="twitter:description" content="二、面向对象基础1. 内存分析1.1 Java虚拟机的内存Java虚拟机的内存可以分为三个区域：栈Stack、堆Heap、方法区method area（也是在堆中）。 1.1.1 栈的特点： 栈是方法执行的内存模型，每个方法被调用后都会创建一个栈帧，栈帧会保存该方法的局部变量、操作数、方法出口等； JVM会为每个线程都开辟一个栈，用于存放该线程执行方法的信息（实际参数、局部变量等），因此，栈时线">
<meta name="twitter:image" content="http://yoursite.com/2018/11/13/Java-300集/assets/java内存模型.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/11/13/Java-300集/">





  <title>Java-300集 | Hexo</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/13/Java-300集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="John Doe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-300集</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-11-13T15:18:10+08:00">
                2018-11-13
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">Post modified&#58;</span>
              
              <time title="Post modified" itemprop="dateModified" datetime="2018-11-21T19:13:50+08:00">
                2018-11-21
              </time>
            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Words count in article&#58;</span>
                
                <span title="Words count in article">
                  803
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Reading time &asymp;</span>
                
                <span title="Reading time">
                  2
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><a name="top"></a></p>
<h1 id="二、面向对象基础"><a href="#二、面向对象基础" class="headerlink" title="二、面向对象基础"></a>二、面向对象基础</h1><h3 id="1-内存分析"><a href="#1-内存分析" class="headerlink" title="1. 内存分析"></a>1. 内存分析</h3><h4 id="1-1-Java虚拟机的内存"><a href="#1-1-Java虚拟机的内存" class="headerlink" title="1.1 Java虚拟机的内存"></a>1.1 Java虚拟机的内存</h4><p>Java虚拟机的内存可以分为三个区域：<strong>栈Stack、堆Heap、方法区method area（也是在堆中）</strong>。</p>
<h5 id="1-1-1-栈的特点："><a href="#1-1-1-栈的特点：" class="headerlink" title="1.1.1 栈的特点："></a>1.1.1 栈的特点：</h5><ul>
<li>栈是方法执行的内存模型，每个方法被调用后都会创建一个栈帧，栈帧会保存该方法的局部变量、操作数、方法出口等；</li>
<li>JVM会为每个线程都开辟一个栈，用于存放该线程执行方法的信息（实际参数、局部变量等），因此，栈时线程私有的，不能线程之间不能共享栈内的数据；</li>
<li>栈由系统自动分配，速度快，属于连续的内存空间；</li>
</ul>
<h5 id="1-1-2-堆的特点："><a href="#1-1-2-堆的特点：" class="headerlink" title="1.1.2 堆的特点："></a>1.1.2 堆的特点：</h5><pre><code>堆是一个不连续的内存空间，分配灵活，速度慢。JVM中只有一个堆，用所有线程之间都共享这个空间。堆用于存储创建好的对象和数组（数组也是对象）。
</code></pre><h5 id="1-1-3-方法区的特点："><a href="#1-1-3-方法区的特点：" class="headerlink" title="1.1.3 方法区的特点："></a>1.1.3 方法区的特点：</h5><ul>
<li>JVM中只有一个方法区，被所有的线程共享；</li>
<li>方法区位于堆中，用于存储类和常量相关的系想你（例如：<strong>类信息Class对象</strong>、<strong>静态变量</strong>、<strong>字符串常量等</strong>）；</li>
</ul>
<h5 id="1-1-4-模型分析"><a href="#1-1-4-模型分析" class="headerlink" title="1.1.4 模型分析"></a>1.1.4 模型分析</h5><p><img src="/2018/11/13/Java-300集/assets\java内存模型.jpg" alt="Java内存模型"></p>
<h3 id="2-垃圾回收机制"><a href="#2-垃圾回收机制" class="headerlink" title="2. 垃圾回收机制"></a>2. 垃圾回收机制</h3><pre><code>Java中的垃圾回收机制中需要做到两点：
</code></pre><ol>
<li>发现内存中无用的对象</li>
<li>清除无用的对象；</li>
</ol>
<p>其中，发现的方法主要由两种：① 引用计数法； ② 引用可达法（根搜索法）。</p>
<h3 id="3-通用分代垃圾回收机制"><a href="#3-通用分代垃圾回收机制" class="headerlink" title="3. 通用分代垃圾回收机制"></a>3. 通用分代垃圾回收机制</h3><pre><code>了解分代垃圾回收机制
</code></pre><h3 id="易遗忘知识点"><a href="#易遗忘知识点" class="headerlink" title="#. 易遗忘知识点"></a>#. 易遗忘知识点</h3><ul>
<li>静态代码块是用来初始化类对象的；静态代码块的执行顺序根据它所处的位置来决定（按照声明的顺序来执行）。</li>
<li><code>instanceof</code>是一个二元运算符，可以用来测试一个对象是否为一个类的实例，当是该类的实例时，会返回<code>true</code>。（注意：当<code>实例 instanceof 实例对应的类的父类</code>时，返回的也是<code>true</code>.）</li>
<li><code>==</code> 和<code>equals()</code>方法：<ul>
<li><code>==</code>代表比较双方是否相同，如果是基本类型则表示判断值是否相等，如果是引用类型，则表示地址是否相等。</li>
</ul>
</li>
</ul>
<p><a href="#top">回顶部</a></p>
<p><br> <br></p>
<p><br></p>
<h1 id="三、多线程"><a href="#三、多线程" class="headerlink" title="三、多线程"></a>三、多线程</h1><h3 id="3-1-创建线程的方式"><a href="#3-1-创建线程的方式" class="headerlink" title="3.1 创建线程的方式"></a>3.1 创建线程的方式</h3><h4 id="3-1-2-利用Callable接口创建多线程"><a href="#3-1-2-利用Callable接口创建多线程" class="headerlink" title="3.1.2 利用Callable接口创建多线程"></a>3.1.2 利用<code>Callable</code>接口创建多线程</h4><p><strong>步骤</strong>：</p>
<ol>
<li>创建目标对象；<br><code></code></li>
<li>创建执行任务；<br><code>ExecutorService ser = Executors.newFixedThreadPool(1);</code></li>
<li>提交执行；<br><code>Future&lt;Boolean&gt; result1 = ser.submit(目标对象);</code></li>
<li>获取结果；<br><code>boolean r1 = result1.get();</code></li>
<li>关闭服务；<br><code>ser.shutdownNow();</code></li>
</ol>
<h3 id="易遗忘的知识点"><a href="#易遗忘的知识点" class="headerlink" title="#. 易遗忘的知识点"></a>#. 易遗忘的知识点</h3><ol>
<li>Java中多线程用了<strong>静态代理模式</strong>，具体模式就是：<ul>
<li>定义两个角色：目标角色<code>You</code>和代理角色<code>WeddingCompany</code>；</li>
<li>上面的两个角色实现相同的接口<code>Marry</code>；</li>
<li>每个角色都继承<code>Marry</code>接口，并实现对应的方法：<ul>
<li>针对<code>You</code>：根据具体的业务来写；</li>
<li>针对<code>WeddingCompany</code>：保存一个目标对象，并调用目标对象的实现方法；同时，在调用方法之前可以执行一些操作（<strong>例如记录日志</strong>），在调用方法之后也可以执行一些操作（<strong>例如记录日志</strong>）。</li>
</ul>
</li>
</ul>
</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/11/13/JavaWeb-学习总结/" rel="next" title="JavaWeb-学习总结">
                <i class="fa fa-chevron-left"></i> JavaWeb-学习总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/11/13/计算机网络/" rel="prev" title="计算机网络">
                计算机网络 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/head.png" alt="John Doe">
            
              <p class="site-author-name" itemprop="name">John Doe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#二、面向对象基础"><span class="nav-number">1.</span> <span class="nav-text">二、面向对象基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-内存分析"><span class="nav-number">1.0.1.</span> <span class="nav-text">1. 内存分析</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-Java虚拟机的内存"><span class="nav-number">1.0.1.1.</span> <span class="nav-text">1.1 Java虚拟机的内存</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-1-栈的特点："><span class="nav-number">1.0.1.1.1.</span> <span class="nav-text">1.1.1 栈的特点：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-2-堆的特点："><span class="nav-number">1.0.1.1.2.</span> <span class="nav-text">1.1.2 堆的特点：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-3-方法区的特点："><span class="nav-number">1.0.1.1.3.</span> <span class="nav-text">1.1.3 方法区的特点：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-4-模型分析"><span class="nav-number">1.0.1.1.4.</span> <span class="nav-text">1.1.4 模型分析</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-垃圾回收机制"><span class="nav-number">1.0.2.</span> <span class="nav-text">2. 垃圾回收机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-通用分代垃圾回收机制"><span class="nav-number">1.0.3.</span> <span class="nav-text">3. 通用分代垃圾回收机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#易遗忘知识点"><span class="nav-number">1.0.4.</span> <span class="nav-text">#. 易遗忘知识点</span></a></li></ol></li></ol><li class="nav-item nav-level-1"><a class="nav-link" href="#三、多线程"><span class="nav-number">2.</span> <span class="nav-text">三、多线程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-创建线程的方式"><span class="nav-number">2.0.1.</span> <span class="nav-text">3.1 创建线程的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-利用Callable接口创建多线程"><span class="nav-number">2.0.1.1.</span> <span class="nav-text">3.1.2 利用Callable接口创建多线程</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#易遗忘的知识点"><span class="nav-number">2.0.2.</span> <span class="nav-text">#. 易遗忘的知识点</span></a></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">John Doe</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">11.7k</span>
  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  





  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/canvas_sphere.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
